package com.yc.demohoudun.mapper;

import com.yc.demohoudun.entiy.Product;
import com.yc.demohoudun.entiy.Score;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductMapper  {

    @Select(" <script>select * from product" +
            " <where>" +
            " <if test='name != null'>name like concat('%', #{name}, '%')</if>" +
            " <if test='price1 > 0'>and price &gt;= #{price1}</if>" +
            " <if test='price2 > 0'>and price &lt;= #{price2}</if>" +
            "<if test='category != null'>and category = #{category}</if>" +
            " </where>" +
            " limit #{begin}, #{size}" +
            "</script>")
    List<Product> selectByNameAndPrice(@Param("name") String name,
                                       @Param("price1") int price1,
                                       @Param("price2") int price2,
                                       @Param("begin") int begin,
                                       @Param("size") int size,
                                       @Param("category") String category

    );



    @Select(" <script>select count(*) from product" +
            " <where>" +
            " <if test='name != null'>name like concat('%', #{name}, '%')</if>" +
            " <if test='price1 > 0'>and price &gt;= #{price1}</if>" +
            " <if test='price2 > 0'>and price &lt;= #{price2}</if>" +
            " </where>" +
            "</script>")
    int countByNameAndPrice(@Param("name") String name,
                                       @Param("price1") int price1,
                                       @Param("price2") int price2
                                      );


    @Select("select count(*) from product where name=#{name}")
    boolean countByName(String name);

    @Insert("insert into product (name, image, price, category, detail) values (#{name}, #{image}, #{price}, #{category}, #{detail})")
    void insert(Product product);



    @Update("update  product set name=#{name},image=#{image},price=#{price},category=#{category},detail=#{detail} where id=#{id}")
    void updateById(Product product);

    @Select("delete from product where id=#{id}")
    void deleteById(int id);

    @Select("select * from product where id=#{id}")
    Product selectById(int id);

    @Select("select person,comment,score from productscore where productId=#{id}")
    List<Score> selectByproductId(int id);

    @Insert("insert into productscore (product,person,score,comment,productId) values (#{product},#{person},#{score},#{comment},#{productId})")
    void insertsco(Score score1);

    @Select("select * from product where name like concat('%', #{name}, '%')")
    Product selectByname(String name);

    @Insert("insert into Ownproduct(person,product) values (#{username},#{name})")
    void inOwn(String usernewName, String name);
}
